package DataStructures.sort;

/**
 * @author ：ALi
 * @date ：Created in 2021/11/8 8:59
 * @description：插入排序
 * @modified By：
 * @version: $
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 5, 4, -1};
        sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

    }

    public static void sort(int[] array) {
        int temp;//保存要插入变量的值
        int index;//记录插入地方的下标
        for (int i = 1; i < array.length; i++) {
            index = i;
            temp = array[i];
            for (int j = i; j > 0; j--) {
                if (temp < array[j - 1]) {
                    array[j] = array[j - 1];
                    index = j - 1;
                }


            }
            array[index] = temp;
        }
    }

}